﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using System.Reflection;
using Configuration.Templating;
using Configuration.Templating.Scope;
using Configuration.Templating.Context;

namespace Aricie.LuceneSearch.Tests.Components.Config.Templating
{
    public class TemplateProviderTest
    {
        public string Path { get {
            return System.IO.Path.GetDirectoryName(new System.Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath);
        }
        }

        [Fact]
        public void ICanCreateATemplateProvider()
        {
            var TP = new TemplateProvider(Path);
        }

        [Fact]
        public void ICanAskForNonExistingKeys()
        {
            var TP = new TemplateProvider(Path);
            Assert.Equal("undefined", TP.GetValue(new PortalScope(3), new TemplatingContext(new string[] { "Detail", "fr-FR", "en-EN", "Contenu", "10" })));
        }

        [Fact]
        public void ICanSetALowLevelKeyAndRetrieveItAtAHigherLevel()
        {
            var TP = new TemplateProvider(Path);
            
            TP.SetValue(new HostScope(),  new TemplatingContext(new string[] { "Type" }), "LowLevelValue");
            TP.SetValue(new HostScope(), new TemplatingContext(new string[] { "Type", "fr-FR" }), "CultureValue");
            TP.SetValue(new HostScope(), new TemplatingContext(new string[] { "Type", "fr-FR", "Content" }), "ContentValue");
            TP.SetValue(new HostScope(), new TemplatingContext(new string[] { "Type", "fr-FR", "Content", "Module" }), "ModuleValue");

            Assert.Equal("LowLevelValue", TP.GetValue(new HostScope(),  new TemplatingContext(new string[] { "Type" })));
            Assert.Equal(true, TP.ValueExists(new HostScope(), new TemplatingContext(new string[] { "Type" })));
            Assert.Equal("undefined", TP.GetValue(new HostScope(),  new TemplatingContext(new string[] { "TypeAlternate" })));
            Assert.Equal(false, TP.ValueExists(new HostScope(), new TemplatingContext(new string[] { "TypeAlternate" })));

            Assert.Equal("CultureValue", TP.GetValue(new HostScope(), new TemplatingContext(new string[] { "Type", "fr-FR" })));
            Assert.Equal(true, TP.ValueExists(new HostScope(), new TemplatingContext(new string[] { "Type", "fr-FR" })));
            Assert.Equal("LowLevelValue", TP.GetValue(new HostScope(), new TemplatingContext(new string[] { "Type", "en-EN" })));
            Assert.Equal(false, TP.ValueExists(new HostScope(), new TemplatingContext(new string[] { "Type", "en-EN" })));
            Assert.Equal("undefined", TP.GetValue(new HostScope(), new TemplatingContext(new string[] { "TypeAlternate", "fr-FR" })));
            Assert.Equal("undefined", TP.GetValue(new HostScope(), new TemplatingContext(new string[] { "TypeAlternate", "fr-FRAlternate" })));

            Assert.Equal("ContentValue", TP.GetValue(new HostScope(), new TemplatingContext(new string[] { "Type", "fr-FR", "Content" })));
            Assert.Equal(true, TP.ValueExists(new HostScope(), new TemplatingContext(new string[] { "Type", "fr-FR", "Content" })));
            Assert.Equal("CultureValue", TP.GetValue(new HostScope(), new TemplatingContext(new string[] { "Type", "fr-FR", "ContentAlternate" })));
            Assert.Equal(false, TP.ValueExists(new HostScope(), new TemplatingContext(new string[] { "Type", "fr-FR", "ContentAlternate" })));
            Assert.Equal("LowLevelValue", TP.GetValue(new HostScope(), new TemplatingContext(new string[] { "Type", "en-EN", "ContentAlternate" })));
            Assert.Equal(false, TP.ValueExists(new HostScope(), new TemplatingContext(new string[] { "Type", "en-EN", "ContentAlternate" })));
           
           
        }

        [Fact]
        public void ICanSetALowLevelScopeAndRetrieveItAtAHigherLevel()
        {
            var TP = new TemplateProvider(Path);

            TP.SetValue(new HostScope(), new TemplatingContext(new string[] { "Type" }), "LowLevelValue");
            TP.SetValue(new HostScope(), new TemplatingContext(new string[] { "Type", "fr-FR" }), "CultureValue");
            TP.SetValue(new PortalScope(0), new TemplatingContext(new string[] { "Type", "fr-FR"}), "PortalContentValue");
            TP.SetValue(new PortalScope(0), new TemplatingContext(new string[] { "Type", "fr-FR", "Content"}), "PortalModuleValue");

            Assert.Equal("LowLevelValue", TP.GetValue(new PortalScope(0), new TemplatingContext(new string[] { "Type" })));
            Assert.Equal(false, TP.ValueExists(new PortalScope(0), new TemplatingContext(new string[] { "Type" })));

            Assert.Equal("PortalContentValue", TP.GetValue(new PortalScope(0), new TemplatingContext(new string[] { "Type", "fr-FR" })));
            Assert.Equal("LowLevelValue", TP.GetValue(new PortalScope(0), new TemplatingContext(new string[] { "Type", "en-EN", "Content" })));
        }

        [Fact]
        public void NameResolverCanBeChanged()
        {
            var TP = new TemplateProvider(Path);

            Assert.Equal("LuceneSearch.Templates.test.resx", TP.NameResolver("test"));

            TP.NameResolver = (s => s);

            Assert.Equal("test", TP.NameResolver("test"));
        }
       
    }
}
